package com.gljx.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.RepTurnexamCost;
import com.gljx.web.Entity.RepTurnexamCostEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.RepTurnexamCostEntryMapper;
import com.gljx.web.dao.RepTurnexamCostMapper;
import com.gljx.web.service.RepTurnexamCostService;

@Service
@Transactional
public class RepTurnexamCostServiceImpl extends CommonDao implements RepTurnexamCostService{

	@Autowired
	RepTurnexamCostEntryMapper repTurnexamCostEntryMapper;
    @Autowired
    private RepTurnexamCostMapper repTurnexamCostMapper;

    /**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.RepTurnexamCostMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.RepTurnexamCostMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<RepTurnexamCostEntry> selectRepTurnexamCostEntry(String fparentId, String studentName,
			String cfidcardno, String replaceName, String replacecardno) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_fparentid", fparentId);
		paramMap.put("v_studentName", studentName);
		paramMap.put("v_cfidcardno", cfidcardno);
		paramMap.put("v_replaceName", replaceName);
		paramMap.put("v_replacecardno", replacecardno);
		paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.RepTurnexamCostEntryMapper.selectRepTurnexamCostEntry", paramMap, null);
		@SuppressWarnings("unchecked")
		List<RepTurnexamCostEntry> repTurnexamCostEntrys = (List<RepTurnexamCostEntry>) paramMap.get("v_cursor");
		return repTurnexamCostEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editRepTurnexamCost(RepTurnexamCost repTurnexamCost, String[] fseq, String[] cfstudentno,
			String[] cfstudentid, String[] cfidcardno, String[] cfguifei, String[] cfthstudentno, String[] cfthxyid,
			String[] cfthwidcardno, String[] cfdesc, User user, String time) {
		// TODO Auto-generated method stub
		if("".equals(repTurnexamCost.getFid()) || repTurnexamCost.getFid() == null){
			//创建人id
			repTurnexamCost.setFcreatorid(user.getFid());
			//利用UUID生成fid
			repTurnexamCost.setFid(GetFid.UUIDIntoFid());
			//当前时间
			repTurnexamCost.setFcreatetime(time);
			//状态
            repTurnexamCost.setCfbillstate(3L);
            //添加
			repTurnexamCostMapper.insertSelective(repTurnexamCost);
		}else{
			//修改时间时间
			repTurnexamCost.setFlastupdatetime(time);
			//修改人id
			repTurnexamCost.setFlastupdateuserid(user.getFid());
			//修改
			repTurnexamCostMapper.updateByPrimaryKeySelective(repTurnexamCost);
			//先删除子类信息
			repTurnexamCostEntryMapper.deleteByFparentId(repTurnexamCost.getFid());
		}
		//添加子类
		List<RepTurnexamCostEntry> repTurnexamCostEntrys = new ArrayList<RepTurnexamCostEntry>();
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				RepTurnexamCostEntry repTurnexamCostEntry = new RepTurnexamCostEntry();
				repTurnexamCostEntry.setCfstudentno(cfstudentno[i]);
				repTurnexamCostEntry.setCfstudentid(cfstudentid[i]);
				repTurnexamCostEntry.setCfidcardno(cfidcardno[i]);
				if("".equals(cfguifei[i])){
					
				}else{
					repTurnexamCostEntry.setCfguifei(BigDecimalFormat.formot(cfguifei[i]));
				}
				repTurnexamCostEntry.setCfthstudentno(cfthstudentno[i]);
				repTurnexamCostEntry.setCfthxyid(cfthxyid[i]);
				repTurnexamCostEntry.setCfthwidcardno(cfthwidcardno[i]);
				repTurnexamCostEntry.setCfdesc(cfdesc[i]);
				repTurnexamCostEntry.setFseq((long)i+1);
				repTurnexamCostEntry.setFid(GetFid.UUIDIntoFid());
				repTurnexamCostEntry.setFparentid(repTurnexamCost.getFid());
				repTurnexamCostEntrys.add(repTurnexamCostEntry);
			}
			//添加
			repTurnexamCostEntryMapper.insertRepTurnexamCostEntry(repTurnexamCostEntrys);
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public RepTurnexamCost queryRepTurnexamCostByFid(String fid) {
		// TODO Auto-generated method stub
		return repTurnexamCostMapper.selectRepTurnexamCostByFid(fid);
	}

	/**
	 * 根据fid删除
	 */
	@Override
	public void deleteRepTurnexamCostByFid(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			//先删除子类信息
			repTurnexamCostEntryMapper.deleteByArray(deleteId);
			//在删除父类信息
			repTurnexamCostMapper.deleteByArray(deleteId);
		}else{
			//先删除子类信息
			repTurnexamCostEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			repTurnexamCostMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 审核,反审核
	 */
	@Override
	public int auditRepTurnexamCost(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
		// TODO Auto-generated method stub
		RepTurnexamCost repTurnexamCost = new RepTurnexamCost();
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            if (serverResponse.getStatus() == 1) {
                //fid
                repTurnexamCost.setFid(fid);
                //审核人
                repTurnexamCost.setFauditorid(fhandlerId);
                //修改人
                repTurnexamCost.setFlastupdateuserid(fhandlerId);
                repTurnexamCost.setCfbillstate(5L);
				//结束
				return repTurnexamCostMapper.updateByPrimaryKeySelective(repTurnexamCost);
			} else if (serverResponse.getStatus() == 2) {
                if (!"1".equals(state)) {
                    repTurnexamCost.setFid(fid);
                    repTurnexamCost.setCfbillstate(3L);
                    repTurnexamCostMapper.updateByPrimaryKeySelective(repTurnexamCost);
                }
                //推进
				return 2;
			} else {
				//错误
				return 0;
			}
		}else{
            axamine.againstAuditProcess(fnumber, message);
            repTurnexamCost.setCfbillstate(3L);
            repTurnexamCost.setFid(fid);
            return repTurnexamCostMapper.updateByPrimaryKeySelective(repTurnexamCost);
		}
	}

    @Override
    public int modifyRepTurnexamCost(RepTurnexamCost repTurnexamCost) {
        return repTurnexamCostMapper.updateByPrimaryKeySelective(repTurnexamCost);
    }

    @Override
    public List<RepTurnexamCostEntry> selectRepTurnexamCostByInfo(String fcompanyid, String studentName, String cfidcardno, String replaceName, String cfthwidcardno, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("cfidcardno", cfidcardno);
        map.put("replaceName", replaceName);
        map.put("cfthwidcardno", cfthwidcardno);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("fcompanyid", fcompanyid);
        return repTurnexamCostEntryMapper.selectRepTurnexamCostByInfo(map);
    }
}
